package org.example.array;

/**
 * 把有序数组进行去重排序
 *
 * @author 玄鸽
 * @since 2023/9/5
 */
public class SortedArrayDuplicates {

    public static void main(String[] args) {
        System.out.println(removeDuplicates(new int[]{0,1,2,2,3,3,4}));
    }

    private static int removeDuplicates(int[] nums) {
        // 设定边界
        if (nums.length == 0) {
            return 0;
        }
        // 慢指针为0
        int i = 0;
        // 快指针为1
        for (int j = 1; j < nums.length; j++) {
            // 如果快慢指针不相等
            if (nums[i] != nums[j]){
                // 慢指针+1，快指针下标的值给慢指针下标的值
                i++;
                nums[i] = nums[j];
            }
        }
        return i + 1;
    }
}
